from models import Measurement, have_trend_fields, text_fields, custom_fields
from fiteat.utils import export_func, import_func, map_fields_func
from fiteat.chart import graph as graph_func
from django.utils.translation import ugettext as _

DAYS_FOR_WEIGHTED_AVERAGE = 7

def graph(request, fields, period):
    prefs = request.user.get_profile().measurements_profile
    data = Measurement.objects.filter(user=request.user).order_by('date')
    # TODO: take it from the model!!!
    known_fields = {
        'weight': 'weight',
        'bf_user': '',
        'bf_calc': '',
        'chest': 'length',
        'waist': 'length',
        'hips': 'length',
        'neck': 'length',
        'biceps': 'length',
        'arm': 'length',
        'forearm': 'length',
        'calf': 'length',
        'custom1': 'custom',
        'custom2': 'custom',
        'custom3': 'custom',
        'custom4': 'custom',
        'custom5': 'custom',
    }
    return graph_func(request, fields, period, prefs, data, known_fields)

def export_view(request):
    data = Measurement.objects.filter(user=request.user).order_by('date')
    filename = 'measurements.csv'
    # TODO: take the fields list from the model
    fields = ['date'] + have_trend_fields + text_fields + custom_fields
    return export_func(request, fields, data, filename)

FIELD_CHOICES = (
    ('-', _('---')),
    ('date', _('Date')),
    ('weight', _('Weight')),
    ('bf_user', _('BF %')),
    ('chest', _('Chest')),
    ('waist', _('Waist')),
    ('hips', _('Hips')),
    ('neck', _('Neck')),
    ('biceps', _('Biceps')),
    ('arm', _('Arm')),
    ('forearm', _('Forearm')),
    ('calf', _('Calf')),
    ('comment', _('Comment')),
    ('custom1', _('Custom1')),
    ('custom2', _('Custom2')),
    ('custom3', _('Custom3')),
    ('custom4', _('Custom4')),
    ('custom5', _('Custom5')),
)


def import_view(request):
    return import_func(request, 'measurements/import.html', 'measurements/map_fields.html', FIELD_CHOICES)

def map_fields(request):
    return map_fields_func(request, 'measurements/map_fields.html', FIELD_CHOICES, Measurement, _('measurements'), '/fiteat/admin/')

